import {
  createRouter,
  createWebHashHistory
} from 'vue-router'

const routes = [{
    path: '/',
    name: 'HoMe',
    component: () => import( /* webpackChunkName: "Home" */ '../views/HoMe')
  },
  {
    path: '/cartList',
    name: 'CartList',
    component: () => import( /* webpackChunkName: "cartList" */ '../views/cartList/CartList')
  },
  {
    path: '/orderList',
    name: 'OrderList',
    component: () => import( /* webpackChunkName: "orderList" */ '../views/orderList/OrderList')
  },
  {
    path: '/personalInfo',
    name: 'PersonalInfo',
    component: () => import( /* webpackChunkName: "personalInfo" */ '../views/personalInfo/PersonalInfo')
  },
  {
    path: '/orderConfirmation/:id',
    name: 'OrderConfirmation',
    component: () => import( /* webpackChunkName: "orderConfirmation" */ '../views/orderConfirmation/OrderConfirmation')
  }, {
    path: '/shop/:id',
    name: 'ShOp',
    component: () => import( /* webpackChunkName: "shop" */ '../views/shop/ShOp')
  },
  {
    path: '/register',
    name: 'ReGister',
    component: () => import( /* webpackChunkName: "register" */ '../views/register/ReGister')
  },
  {
    path: '/search',
    name: 'SearCh',
    component: () => import( /* webpackChunkName: "search" */ '../views/search/SearCh')
  },
  {
    path: '/searchList',
    name: 'SearchList',
    component: () => import( /* webpackChunkName: "searchList" */ '../views/searchList/SearchList')
  },
  {
    path: '/myAddressList',
    name: 'MyAddressList',
    component: () => import( /* webpackChunkName: "myAddressList" */ '../views/myAddressList/MyAddressList')
  },
  {
    path: '/addAddress',
    name: 'AddAddress',
    component: () => import( /* webpackChunkName: "addAddress" */ '../views/addAddress/AddAddress')
  },
  {
    path: '/upsertAddress',
    name: 'UpsertAddress',
    component: () => import( /* webpackChunkName: "addAddress" */ '../views/upsertAddress/UpsertAddress')
  },
  {
    path: '/login',
    name: 'LoGin',
    component: () => import( /* webpackChunkName: "login" */ '../views/login/LoGin'),
    beforeEnter(to, from, next) {
      const {
        isLogin
      } = localStorage
      isLogin ? next({
        name: 'HoMe'
      }) : next()
    }
  },

  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () => import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  const {
    isLogin
  } = localStorage
  const {
    name
  } = to
  const isLoginOrRegister = name === 'LoGin' || name === 'ReGister'
  isLogin || isLoginOrRegister ? next() : next({
    name: 'LoGin'
  })
})

export default router